package com.neusoft.bizcore.notice.notification.converter;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.notice.notification.dto.NoticeRecordMailDTO;
import com.neusoft.bizcore.notice.notification.model.NoticeRecordMail;
import com.neusoft.bizcore.notice.notification.model.enums.SendStatus;
import com.neusoft.bizcore.notice.notification.repository.NoticeRecordMailRepository;
import com.neusoft.bizcore.web.dto.AbstractConverter;

@Component
public class NoticeRecordMailConverter extends AbstractConverter<NoticeRecordMail, NoticeRecordMailDTO> {

    @Autowired
    private NoticeRecordMailRepository mailRepository;

    @Override
    public NoticeRecordMail toModel(final NoticeRecordMailDTO dto) {
        NoticeRecordMail model;
        if (dto.isNew()) {
            model = new NoticeRecordMail();
        } else {
            model = this.mailRepository.getOne(dto.getId());
        }
        model.setEmailAddress(dto.getEmailAddress());
        model.setErrorMsg(dto.getErrorMsg());
        model.setTemplateMsg(dto.getTemplateMsg());
        model.setTemplateName(dto.getTemplateName());
        model.setStatus(SendStatus.valueOf(dto.getStatus()));
        return model;
    }

    @Override
    public NoticeRecordMailDTO toDTO(final NoticeRecordMail model, final boolean forListView) {
        final NoticeRecordMailDTO dto = new NoticeRecordMailDTO();
        dto.setEmailAddress(model.getEmailAddress());
        dto.setErrorMsg(model.getErrorMsg());
        dto.setId(model.getId());
        dto.setStatus(model.getStatus().toString());
        dto.setTemplateMsg(model.getTemplateMsg());
        dto.setTemplateName(model.getTemplateName());
        return dto;
    }

}
